import pandas as pd
import re

df_1 = pd.read_csv('../static/data/info_1.csv')
df_2 = pd.read_csv('../static/data/info_2.csv')
df_3 = pd.read_csv('../static/data/info_3.csv')
df_4 = pd.read_csv('../static/data/info_4.csv')
df_5 = pd.read_csv('../static/data/info_5.csv')
df = pd.concat([df_1, df_2, df_3, df_4, df_5], axis=0)
df['address'] = df['address'].apply(lambda x: x.replace(' ', ''))
df['region'] = df['address'].apply(lambda x: re.findall('\[(.*?)\]', x)[0][:2] + '区')
df['sale_status'] = df['tags_wrap_1'].apply(lambda x: x.split('|')[0] if len(x.split('|')) == 2 else '无')
df['dwelling_type'] = df['tags_wrap_1'].apply(lambda x: x.split('|')[1] if len(x.split('|')) == 2 else '无')
df['house_type'] = df['house_type'].apply(lambda x: '' if pd.isnull(x) else x)
df['unit_type'] = df['house_type'].apply(lambda x: '|'.join(re.findall('\d室', x)))
df['area'] = df['house_type'].apply(lambda x: ''.join(re.findall('建筑面积：(.*?)㎡', x)))
df['unit_type'] = df['unit_type'].apply(lambda x: '无' if x == '' else x)
df['price'] = df['price'].apply(lambda x: 0 if pd.isnull(x) else x)
df.rename({'tags_wrap_2': 'tags'}, axis=1, inplace=True)
df = df[['title', 'address', 'tags', 'price', 'region', 'sale_status', 'dwelling_type', 'unit_type', 'area']]
df.to_csv('../static/data/info_pre.csv', index=False)
